﻿using System;

namespace DimensionsHelper.Common.Contracts.Excel.Options;

/// <summary>
///     配置Excel单元格样式的各种参数
/// </summary>
[Serializable]
public class RangeStyleOptions
{
    /// <summary>
    ///     单元格字体样式。
    /// </summary>
    public FontStyleOptions Font { get; set; } = new FontStyleOptions();

    /// <summary>
    ///     水平对齐样式。
    /// </summary>
    public ExcelHAlign HAlign { get; set; } = ExcelHAlign.General;

    /// <summary>
    ///     垂直对齐样式。
    /// </summary>
    public ExcelVAlign VAlign { get; set; } = ExcelVAlign.Center;

    /// <summary>
    ///     单元格的背景颜色，如果不添加，设置为<see cref="ExcelStyleColor.Empty" />
    /// </summary>
    public ExcelStyleColor InteriorColor { get; set; } = ExcelStyleColor.Empty;

    /// <summary>
    ///     区域的边框位置，默认是<see cref="BorderPosition.None" />
    /// </summary>
    public BorderPosition BorderPosition { get; set; } = BorderPosition.None;

    /// <summary>
    ///     区域的边框样式，默认是<see cref="BorderStyle.Thin" />
    /// </summary>
    public BorderStyle BorderStyle { get; set; } = BorderStyle.Thin;

    /// <summary>
    ///     单元格的数值类型，如果不修改，设置为<see cref="string.Empty" />。
    ///     此格式和Excel的配置格式相同，例如，如果配置保留1位小数，填写"0.0_ "，
    ///     如果只显示整数，填写"0_ "
    /// </summary>
    public string NumberFormatLocal { get; set; } = string.Empty;

    /// <summary>
    ///     是否配置区域文本自动换行。
    /// </summary>
    public bool WrapText
    {
        get; set;
    }

    /// <summary>
    ///     将给定单元格样式和当前配置合并
    /// </summary>
    /// <param name="styleOptions">已有的单元格样式</param>
    /// <returns>合并后的样式</returns>
    public RangeStyleOptions Merge(RangeStyleOptions styleOptions)
    {
        return new RangeStyleOptions
        {
            Font = Font.Merge(styleOptions.Font),
            InteriorColor = InteriorColor == ExcelStyleColor.Empty ? styleOptions.InteriorColor : InteriorColor,
            BorderPosition = BorderPosition == BorderPosition.None ? styleOptions.BorderPosition : BorderPosition,
            BorderStyle = BorderStyle == BorderStyle.None ? styleOptions.BorderStyle : BorderStyle,
            HAlign = HAlign,
            VAlign = VAlign,
            NumberFormatLocal = string.IsNullOrEmpty(NumberFormatLocal)
                ? styleOptions.NumberFormatLocal
                : NumberFormatLocal,
            WrapText = WrapText || styleOptions.WrapText
        };
    }


    public RangeStyleOptions GetOnlyRangeStyleOptions()
    {
        return new RangeStyleOptions
        {
            HAlign = HAlign,
            VAlign = VAlign,
            InteriorColor = InteriorColor,
            BorderPosition = BorderPosition,
            BorderStyle = BorderStyle,
            NumberFormatLocal = NumberFormatLocal,
            WrapText = WrapText
        };
    }
}